package com.screen.screendisplay.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.dryport.DryportOpermonCar;
import com.screen.screendisplay.mapper.dryport.DryportOpermonCarMapper;
import com.screen.screendisplay.service.dryport.DryportOpermonCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.dryport.VehicleDetailVO;
import com.screen.screendisplay.vo.dryport.VehicleStatusVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机车车辆运行情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportOpermonCarServiceImpl extends ServiceImpl<DryportOpermonCarMapper, DryportOpermonCar> implements DryportOpermonCarService {

    @Override
    public VehicleStatusVO getVehicleStatus(Integer year) {
        // 设置默认年份为2025
        if (year == null) {
            year = 2025;
        }
        
        // 查询指定年份的数据
        LambdaQueryWrapper<DryportOpermonCar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DryportOpermonCar::getYear, year);
        queryWrapper.last("LIMIT 1"); // 只取一条记录
        DryportOpermonCar entity = this.getOne(queryWrapper);
        
        VehicleStatusVO response = new VehicleStatusVO();
        
        // 如果没有数据，返回空对象
        if (entity == null) {
            response.setDataList(new ArrayList<>());
            return response;
        }
        
        // 设置总设备量和各车型占比
        response.setTotalEquipment(entity.getTotalEquipment());
        response.setKv80Ratio(entity.getKv80Ratio());
        response.setC70eRatio(entity.getC70eRatio());
        response.setC70Ratio(entity.getC70Ratio());
        
        // 转换详细数据列表 - 从一条记录中提取三种车型的数据
        List<VehicleDetailVO> dataList = new ArrayList<>();
        
        // 创建KV80数据
        VehicleDetailVO kv80Detail = new VehicleDetailVO();
        kv80Detail.setYear(entity.getYear());
        kv80Detail.setMonth(entity.getMonth());
        kv80Detail.setVehicleType("KV80");
        kv80Detail.setCount(entity.getKv80Count());
        kv80Detail.setInUse(entity.getKv80InUse());
        kv80Detail.setFactoryRepair(entity.getKv80FactoryRepair());
        kv80Detail.setDepotRepair(entity.getKv80DepotRepair());
        kv80Detail.setTempRepair(entity.getKv80TempRepair());
        dataList.add(kv80Detail);
        
        // 创建C70E数据
        VehicleDetailVO c70eDetail = new VehicleDetailVO();
        c70eDetail.setYear(entity.getYear());
        c70eDetail.setMonth(entity.getMonth());
        c70eDetail.setVehicleType("C70E");
        c70eDetail.setCount(entity.getC70eCount());
        c70eDetail.setInUse(entity.getC70eInUse());
        c70eDetail.setFactoryRepair(entity.getC70eFactoryRepair());
        c70eDetail.setDepotRepair(entity.getC70eDepotRepair());
        c70eDetail.setTempRepair(entity.getC70eTempRepair());
        dataList.add(c70eDetail);
        
        // 创建C70数据
        VehicleDetailVO c70Detail = new VehicleDetailVO();
        c70Detail.setYear(entity.getYear());
        c70Detail.setMonth(entity.getMonth());
        c70Detail.setVehicleType("C70");
        c70Detail.setCount(entity.getC70Count());
        c70Detail.setInUse(entity.getC70InUse());
        c70Detail.setFactoryRepair(entity.getC70FactoryRepair());
        c70Detail.setDepotRepair(entity.getC70DepotRepair());
        c70Detail.setTempRepair(entity.getC70TempRepair());
        dataList.add(c70Detail);
        
        response.setDataList(dataList);
        
        return response;
    }

}
